var express = require('express');
var router = express.Router();
const User = require('../model/User');
const validator = require('validator');
const Dbset = require('../model/db');
const Games = require('../model/Games');
const Reply = require('../model/Reply');
const Article = require('../model/Article')
const setting = require('../setting');
const saveMessage = require('../common/message');
var multer = require('multer')
var picture = []
var storage = multer.diskStorage({
    //文件存储的路径
    destination: function (req, file, cb) {
        cb(null, './public/upload')
    },
    //文件的名称设置
    filename: function (req, file, cb) {
        var suffix = file.mimetype.split("/")[1];
        var fileName = file.fieldname + '-' + Date.now() + "." + suffix;
        picture.push(fileName)
        cb(null, fileName);
    }
})
//通过multer处理当前文件的存储
var upload = multer({ storage: storage });


router.post('/touxiang/:ids', upload.single('img'), function (req, res) {
    console.log('------------------------------')
    // console.log(req.file)
    console.log(req.body)
    User.findById(req.body.ids, function (err, user) {
        // console.log(user)
        if (err) {
            return res.json({ error: 1, message: "simg出错" })
        }
        req.body.url = picture
        console.log(req.body.url)
        user.avatar = req.body.url
        user.save()
        res.json({ error: 0, user: user })
    })
})



module.exports = router;